﻿using Newtonsoft.Json.Linq;
using SilenthinkContract.Domain;

namespace Web.OAuth.Client.Impl
{
    public class SinaClient : OAuthClient
    {
        private ExternalUser user;
        public SinaClient(OAuthConfig configuration)
            : base(configuration)
        {
        }

        #region Overrides of OAuthClient

        /// <summary>
        /// Should return parsed <see cref="ExternalUser"/> using content received from provider.
        /// </summary>
        /// <param name="content">The content which is received from provider.</param>
        protected override ExternalUser ParseUserInfo(string content)
        {
            return user;
        }
        protected override void BeforeGetUserInfo(RestSharp.IRestRequest request)
        {
            return;
        }
        protected override void AfterGetAccessToken(RestSharp.IRestResponse response)
        {
            var content = response.Content;
            var result = JObject.Parse(content);
            user = new ExternalUser { Id = result["uid"].Value<string>(), ProviderName = ProviderName };
        }
        #endregion
    }
}